package com.bezdelnick.forum.web.panel;

import com.bezdelnick.forum.service.ForumService;
import com.bezdelnick.forum.web.component.LinkWithText;
import com.bezdelnick.forum.web.page.LoginPage;
import com.bezdelnick.forum.web.page.UserRegistrationPage;
import org.apache.wicket.authroles.authentication.AuthenticatedWebSession;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;

import java.io.Serializable;
import java.util.Collection;

/**
 * Created by IntelliJ IDEA.
 * User: Bezdelnick
 * Date: 29.08.12
 * Time: 10:51
 * To change this template use File | Settings | File Templates.
 */
public class UserInfoPanel extends Panel {
    private static final Logger LOGGER = LoggerFactory.getLogger(UserInfoPanel.class);

    @SpringBean
    @SuppressWarnings("unused")
    private ForumService forumService;

    private String name;
    private String roles = "";

    public UserInfoPanel(String id) {
        super(id);

        AuthenticatedWebSession session = AuthenticatedWebSession.get();
        try {
            if (session.isSignedIn()) {
                User currentUser = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
                this.name = currentUser.getUsername();

                Collection<GrantedAuthority> authorities = currentUser.getAuthorities();
                for (GrantedAuthority authority : authorities) {
                    this.roles += authority.getAuthority() + " ";
                }

                add(new Label("name", this.name));
                add(new Label("roles", this.roles));
            } else
                this.showAnonymouseUser();
        } catch (Exception ex) {
            this.showAnonymouseUser();
            LOGGER.error(ex.getMessage());
        }

        this.showButtons();
    }

    private void showButtons() {
        BookmarkablePageLink gotoUserRegistration = new BookmarkablePageLink("goto_registration", UserRegistrationPage.class);
        gotoUserRegistration.setBody(new Model<Serializable>("Registration"));
        add(gotoUserRegistration);

        LinkWithText gotoLoginPage = new LinkWithText("goto_login_page", "#", "Login"){
            @Override
            public void onClick() {
                //PageParameters
                PageParameters pageParameters = new PageParameters();
                pageParameters.add("userRequest", "ok");
                pageParameters.add("exp", "user");
                LoginPage loginPage = new LoginPage(pageParameters);
//                loginPage.getPageParameters().add("userRequest", "ok");
//                loginPage.getPageParameters().add("exp", "user");
                redirectToInterceptPage(loginPage);
            }
        };
        add(gotoLoginPage);

        LinkWithText gotoLogout = new LinkWithText("goto_logout", "/logout", "Logout") {
            @Override
            public void onClick() {
                AuthenticatedWebSession session = AuthenticatedWebSession.get();
                session.signOut();
                setResponsePage(getApplication().getHomePage());
            }
        };
        add(gotoLogout);
    }

    private void showAnonymouseUser() {
        add(new Label("name", "Anonymous"));
        add(new Label("roles", "---"));
    }
}
